public class 寻找二叉搜索树中的目标节点 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    
    class Solution {
        int cnts,ans;
        public int findTargetNode(TreeNode root, int cnt) {
            cnts = cnt;
            dfs(root);
            return ans;
        }

        public void dfs(TreeNode root) {
            if (root == null) {
                return;
            }
            dfs(root.right);
            if (cnts == 0) {
                return ;
            }
            if (--cnts == 0) {
                ans = root.val;
            }
            dfs(root.left);


        }
    }
}
